package jm.demo.script.groovy;

import groovy.lang.Binding;
import groovy.lang.GroovyShell;
import groovy.lang.Script;
import groovy.util.GroovyScriptEngine;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

import jm.demo.auth.domain.UserInterface;

import org.apache.commons.beanutils.PropertyUtils;
import org.springframework.util.ResourceUtils;
import org.springframework.util.StopWatch;

public class GSEDemo {
    
    private static GroovyScriptEngine extracted() throws IOException,
            MalformedURLException {
        String file = "groovy/User.groovy";
        URL url = ResourceUtils.getURL("classpath:" + file);
        System.out.println(url.toString());
        String gse_url = url.toString().replaceAll(file, "");
        System.out.println(gse_url);
        GroovyScriptEngine gse = new GroovyScriptEngine(new URL[] { new URL(gse_url) });
//        GroovyScriptEngine gse = new GroovyScriptEngine(new String[] { 
//                "F:/backup/01_jm/code/jm-demo/jm-demo/java/JPADemo/src/main/groovy"
//                , "F:/backup/01_jm/code/jm-demo/jm-demo/java/JPADemo/src/main/resources"
//        });
        return gse;
    }
    

    public static void testClassPathLoad() throws Exception {
        GroovyScriptEngine gse = extracted();
        Class clazz = gse.loadScriptByName("groovy/User.groovy");
        Object aScript = clazz.newInstance();
        UserInterface user = (UserInterface) aScript;
        System.out.println(user.getId());
        System.out.println(user.getName());
        System.out.println(clazz.getName());

    }

    public static void testRunScript() throws Exception {
        GroovyScriptEngine gse = extracted();
        Binding binding = new Binding();
        binding.setVariable("input", "world");
        binding.setProperty("pName", "jiming");
        gse.run("groovy/echo.groovy", binding);
        System.out.println(binding.getVariable("output"));
        System.out.println(binding.getVariable("userDetail"));
        System.out.println(PropertyUtils.getProperty(binding.getVariable("user"), "id"));
        
        
    }
    
    public static void testScriptPerf() throws Exception {
        Binding binding = new Binding();
        GroovyShell shell = new GroovyShell();
        int script_counter = 0;
        binding.setVariable("counter", script_counter);

        StopWatch stopWatch = new StopWatch();
        
        Script script = shell.parse("result=(counter++ % 10)");
        // Script script = shell.parse("result=System.currentTimeMillis()");
        
        
        long counter = 1000;

        stopWatch.start("preheat - " + counter);
        
        script.setBinding(binding);
        for(int i = 0; i < counter; i++) {
            Object run = script.run();
            System.out.println(binding.getVariable("result") + "::" + script_counter + "::" + run);
        }
        stopWatch.stop();
        
        counter = 1000000;
        stopWatch.start("real - " + counter);
        for(int i = 0; i < counter; i++) {
            script.run();
        }
        System.out.println(binding.getVariable("counter"));
        stopWatch.stop();
        
        System.out.println(stopWatch.prettyPrint());
        
    }
    
    /**
     * @param args
     */
    public static void main(String[] args) {
        try {
            // testClassPathLoad();
            // testRunScript();
            testScriptPerf();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

}
